<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Checker;
use App\Http\Requests\CheckersRequest;
use Illuminate\Support\Facades\Validator;

class CheckerController extends Controller
{
    public function store(Request $request, Checker $checker)
    {
        $checkerRequest = new CheckersRequest();
        $validator = Validator::make($request->all(),$checkerRequest->rules(), $checkerRequest->messages());
        if ($validator->fails()){
            return response()->json(['error' => $validator->errors()->first()]);
        }
        $checker->fill($request->all());
        $checker->save();
        return response()->json();
    }

    public function check(Request $request, Checker $checker)
    {
        $data = [
            'is_checker' => false,
        ];
        if (!$request->openid){
            return response()->json(['error' => '缺少必要参数']);
        }
        $one = Checker::where('openid', $request->openid)->count();
        if ($one){
            $data['is_checker'] = true;
        }

        return response()->json($data);
    }
}
